<template>
  <div class="login">
    <p>登录页面</p>
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="用户姓名：">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="用户密码：">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="submit">登录</el-button>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        username: '',
        password: '',
      }
    }
  },
  methods: {
    submit () {
      // 请求登录接口
      let postData = {
        username: this.form.username,
        password: this.form.password,
      }
      console.log('postData', postData)
      this.axios({
        method: 'post',
        url: '/user/login',
        // 利用params接收参数
        params: postData
      }).then(res => {
        console.log(res)
        if (res.data.code == "200") {
          this.$message({
            type: 'success',
            message: '登录成功!'
          })
          // 并缓存token
          // 跳转到用户页面
          this.$router.push("/user")
        } else {
          this.$message({
            type: 'error',
            message: '登录失败!'
          })
        }
      })
    }
  },
  created () {

  },
}
</script>
<style scoped >
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  flex-direction: column;
}
</style>
